#include"Protocol.hpp"
#include"Sock.hpp"
/*
int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        cout << "Usage: \n\t" << argv[0] << "ip" << "port" << endl;
        exit(1);
    }
    int sock = Sock::Socket();

    Sock::Connect(sock,argv[1],atoi(argv[2]));
    
    request_t req;
    memset(&req, 0, sizeof(req));
    cout<<"Please enter first data: ";
    cin>>req.x;
    cout<<"Please enter second data: ";
    cin>>req.op;
    cout<<"Please enter third data: ";
    cin>>req.y;

    write(sock, &req, sizeof(req));
    
    response_t resp;
    memset(&resp, 0, sizeof(resp));
    ssize_t s = read(sock, &resp, sizeof(resp));
    cout << "answer of code: " << resp.code << endl;
    cout << "answer of result: " << resp.result << endl;

    close(sock);
}
*/
int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        cout << "Usage: \n\t" << argv[0] << "ip" << "port" << endl;
        exit(1);
    }
    int sock = Sock::Socket();

    Sock::Connect(sock,argv[1],atoi(argv[2]));
    
    request_t req;
    memset(&req, 0, sizeof(req));
    cout<<"Please enter first data: ";
    cin>>req.x;
    cout<<"Please enter second data: ";
    cin>>req.op;
    cout<<"Please enter third data: ";
    cin>>req.y;
    //序列化
    string req_string = SerializeRequest(req);
    write(sock, req_string.c_str(), req_string.size());
    




    char buffer[1024];
    ssize_t s = read(sock, buffer, sizeof(buffer)-1);
    if(s > 0)
    {
        buffer[s] = 0;
        response_t resp;
        memset(&resp, 0, sizeof(resp));
        string response_stirng = buffer;
        DeserializeResponse(response_stirng, resp);

        cout << "answer of code: " << resp.code << endl;
        cout << "answer of result: " << resp.result << endl;
    }

    close(sock);
}